package javabean;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.HashMap;
import java.util.Map;

import net.sf.json.JSONArray;
import net.sf.json.JSONObject;

public class Manager {
	@SuppressWarnings("null")
	public String login(String user, String psw) throws ClassNotFoundException, SQLException {

		if (user == null || user.trim().equals("")) {
			return "账号不能为空";
		} else if (psw == null || psw.trim().equals("")) {
			return "密码不能为空";
		}
		Connection connection = null;
		PreparedStatement pstmt = null;
		ResultSet resultSet = null;
		String sql = "select * from postman where Pid=? and Ppassword=?";
		connection = Base.getConnection();
		pstmt = (PreparedStatement) connection.prepareStatement(sql);
		pstmt.setString(1, user);
		pstmt.setString(2, psw);
		resultSet = pstmt.executeQuery();
		if (resultSet.next()) {
			return "1";
		}
		return "账号或密码错误";
	}

	public String order_receiving(int oid) throws ClassNotFoundException, SQLException{
		Connection connection = null;
		PreparedStatement pstmt = null;
		ResultSet resultSet = null;
		String sql = "update oorder set state='transporting' where Oid=oid";
		connection = Base.getConnection();
		pstmt = (PreparedStatement) connection.prepareStatement(sql);
		resultSet = pstmt.executeQuery();
		if (resultSet.next()) {
			return "状态更新成功";
		}
		return "状态更新失败";
	}
	/**
	 * 获取图书列表
	 * @param page
	 * @param limit
	 * @return String json字符串
	 * @throws ClassNotFoundException
	 * @throws SQLException
	 */
	public Map getorderList(String page, String limit, Map where,String Pid) throws ClassNotFoundException, SQLException {
		Map<String, Object> map = new HashMap<String, Object>();
		String whereString = "";
		Connection connection = null;
		PreparedStatement pstmt = null;
		ResultSet resultSet = null;
		connection = Base.getConnection();
		int number = Integer.valueOf(page);
		int size = Integer.valueOf(limit);
		String sql = "select * from  "+Pid+"delivery ";
		if(where!=null && !where.isEmpty()) {
			whereString += " where "+where.get("condition") +" like '%" +where.get("conditionValue") +"%' ";
			sql += whereString;
		}
		sql += "order by Oid desc limit ?,? ";
		pstmt = (PreparedStatement) connection.prepareStatement(sql);
		pstmt.setInt(1, (number-1) * size );
		pstmt.setInt(2, size);
		System.out.println(sql);
		resultSet = pstmt.executeQuery();
		System.out.println("????-------");
		JSONObject json = new JSONObject();

		String result = "";
		int i = 1;

		// 获取行数据
		while( resultSet.next() ) {

			json.put("Oid", resultSet.getString("Oid"));
			json.put("destination", resultSet.getString("destination"));
			json.put("detail", resultSet.getString("detail"));
			json.put("Uid", resultSet.getString("Uid"));
			json.put("Rid", resultSet.getString("Rid"));
			json.put("nowarea", resultSet.getString("nowarea"));
			json.put("state", resultSet.getString("state"));
			json.put("time", resultSet.getString("time"));
			if(i==1) {
				result = json.toString();
			}else {
				result += "," +json.toString();
			}
			i++;
		}
		map.put("data", result);

		// 获取总数count，重写sql
		int count = 0;
		sql = "select count(*) as count  from oorder ";
		if(where!=null && !where.isEmpty()) {
			sql += whereString;
		}
		pstmt = connection.prepareStatement(sql);
		resultSet = pstmt.executeQuery();
		if(resultSet.next()) {
			count = resultSet.getInt("count");
		}
		map.put("count", count);
		Base.closeResource(connection, pstmt, resultSet);
		return map;

	}

	/**
	 * 通过id(String)获取分类名称
	 * @param Oid
	 * @return
	 * @throws ClassNotFoundException
	 * @throws SQLException
	 */
	public static String getStateName(String Oid) throws ClassNotFoundException, SQLException {
		if(Oid==null || Oid.equals(""))
			return "参数错误";
		Connection connection = null;
		PreparedStatement pstmt = null;
		ResultSet resultSet = null;
		String sql = null;
		String result = null;
		connection =  Base.getConnection();
		sql = "select state from oorder where Oid=?";
		pstmt = connection.prepareStatement(sql);
		pstmt.setString(1, Oid);
		resultSet = pstmt.executeQuery();
		if(resultSet.next()) {
			result = resultSet.getString("state");
		}else {
			result = "查询失败";
		}
		Base.closeResource(connection, pstmt, null);
		return result;
	}

	/**
	 * 根据id获取书籍信息
	 * @param Oid
	 * @return
	 * @throws ClassNotFoundException
	 * @throws SQLException
	 */
	public static ResultSet getorderInfo(int Oid) throws ClassNotFoundException, SQLException {
		Connection connection = null;
		PreparedStatement pstmt = null;
		ResultSet resultSet = null;
		String sql = "select * from oorder where Oid=? ";
		connection = Base.getConnection();
		pstmt = connection.prepareStatement(sql);
		pstmt.setInt(1, Oid);
		resultSet = pstmt.executeQuery();
		//Base.closeResource(null, null, null);
		connection.close();
		if(resultSet.next()) {
			return resultSet;
		}

		return null;
	}


}

